// miniprogram/pages/addUser.js
var config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uuid:'',
    phone: '',
    show: false,
    columns: ['默认用户', '指纹用户', '密码用户', 'IC卡用户', '钥匙用户', '手机用户', '人脸用户', '掌纹用户', '虹膜用户', '临时用户'],
   
      userType: '',
      userid: 0,
      usernamme: ''
    
  },
  // 确定
  submit () {
    console.log('submit');
    console.log('usernamme', this.data.usernamme);
    console.log('userid', this.data.userid, "typeof:", typeof this.data.userid);
    console.log('userType', this.data.userType);
    if (this.data.usernamme == '') {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none',
        duration: 2000
      })
    }
    // else if ((this.data.userid > '99999') || (this.data.userid > 99999)) {
    //   wx.showToast({
    //     title: '用户编号不对',
    //     icon: 'none',
    //     duration: 2000
    //   })
    // }
    else if (this.data.userType == '') {
      wx.showToast({
        title: '请选择用户类型',
        icon: 'none',
        duration: 2000
      })
    }
    else {
      this.addReUser(this.data.userid, this.data.usernamme, this.data.userType) 
    }

  },
  onInpustUserType(event)
  {
    this.setData({
      userType: event.detail
    })
  },
  onInpustUsercode(event) {
    this.setData({
      userid: event.detail
    })
  },
  onInpustUsernamme(event) {
    this.setData({
      usernamme: event.detail
    })
  },
  showPopup() {
    console.log('showPopup');
    this.setData({ show: true });
  },
  onClose() {
    // console.log('onClose');
    this.setData({ show: false });
  },
  onCancel() {
    console.log('onCancel');
    this.setData({ show: false });
  },
  configPiker (event) {
    const { picker, value, index } = event.detail;
    console.log(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      "userType": value
    })
    this.onClose(
    )
    
  },
  addReUser(user_id, rename, user_type) {
    var _this = this
    wx.request({
      url: config.url.addReUser,
      method: 'POST',
      dataType: "json",
      data: {
        uuid: _this.data.uuid,
        user_id: user_id,
        rename: rename,
        user_type: user_type,
        'client': config.client,
      },
      header: {
        // 'content-type': 'application/json', // 默认值
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success(res) {
        console.log(res.data)
        console.log("code:", res.data.code)
        console.log("length:", res.data.data.length)


        if (res.data.code == 200) {
          _this.setData({
            user_new_add_show: false,
            user_sel_show: false,
            title_add_text: "新增",
          })
         // _this.getReUserList()
          wx.navigateTo({
            url: '../reusername/reusername?uuid=' + _this.data.uuid + '&phone=' + _this.data.phone,
          })
          return
        }
        else {
          wx.showToast({
            title: '添加失败',
            icon: 'none',
            duration: 2000
          })

        }

      },
      fail(e) {
        console.error(e)

      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    console.log("options:", options)
    console.log("uuid:", options.uuid)
    console.log("phone:", options.phone)
    this.setData({
      uuid: options.uuid,
      phone: options.phone
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})